using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace ProjectSurviver
{
	public class UIGamePanelData : UIPanelData
	{
	}
	public partial class UIGamePanel : UIPanel
	{
		// 事件
		public static EasyEvent FlashScreen = new EasyEvent();

		public static EasyEvent OpenTreasurePanel = new EasyEvent();

		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as UIGamePanelData ?? new UIGamePanelData();
			// 注册值变更的事件
			Global.Exp.RegisterWithInitValue(exp =>
			{
				ExpValue.fillAmount = exp / (float)Global.ExpToNextLevel();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.Level.RegisterWithInitValue(lv =>
			{
				LevelText.text = "等级：" + lv;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			ExpUpgradePanel.Hide();

			Global.Level.Register(lv =>
			{
				// 游戏暂停
				Time.timeScale = 0;
				ExpUpgradePanel.Show();
				AudioKit.PlaySound("LevelUp");
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.Exp.RegisterWithInitValue(exp =>
			{
				if (exp >= Global.ExpToNextLevel())
				{
					Global.Exp.Value -= Global.ExpToNextLevel();
					Global.Level.Value++;
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.CurrentScends.RegisterWithInitValue(currentScends =>
			{
				if (Time.frameCount % 30 == 0)
				{
					var currentScendsInt = Mathf.FloorToInt(currentScends);
					var seconds = currentScendsInt % 60;
					var minutes = currentScendsInt / 60;
					TimeText.text = "时间：" + $"{minutes:00} : {seconds:00}";
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			EnemyGenerator.EnemyCount.RegisterWithInitValue(enemyCount =>
			{
				EnemyCountText.text = "敌人：" + enemyCount;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			var enemyGenerator = FindObjectOfType<EnemyGenerator>();
			ActionKit.OnUpdate.Register(() =>
			{
				Global.CurrentScends.Value += Time.deltaTime;
				//游戏通关条件
				if (enemyGenerator.LastWave && enemyGenerator.currentWave == null && EnemyGenerator.EnemyCount.Value == 0)
				{
					UIKit.OpenPanel<UIGamePassPanel>();
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			Global.Coin.RegisterWithInitValue(coin =>
			{
				CoinText.text = "金币：" + coin;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			FlashScreen.Register(() =>
			{
				ActionKit.Sequence()
				.Lerp(0, 0.5f, 0.1f, alpha =>
				{
					ScreenColor.ColorAlpha(alpha);
				})
				.Lerp(0.5f, 0, 0.2f, alpha =>
				{
					ScreenColor.ColorAlpha(alpha);
				}, () => ScreenColor.ColorAlpha(0))
				.Start(this);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			OpenTreasurePanel.Register(() =>
			{
				Time.timeScale = 0.0f;
				TreasureChestPanel.Show();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		protected override void OnOpen(IUIData uiData = null)
		{
		}

		protected override void OnShow()
		{
		}

		protected override void OnHide()
		{
		}

		protected override void OnClose()
		{
		}
	}
}
